package
{
	import flash.display.Graphics;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	
public class RectTool extends MoveTool
{
	protected var rect_handler:Boolean;
	protected var resetting:Boolean;
	protected var focusPoint:Object;
	protected var focusRect:Rect;
	
	protected var dx:Number;
	protected var dy:Number;
	
	public function RectTool()
	{
		super();
		this.keyCode=82;
	}
	
	override public function drawSelectedPoint(point:Object,point_pen:Graphics,mouse_mc:Sprite):void{
		super.drawSelectedPoint(point,point_pen,mouse_mc);
		
		if(!point.isRect)return;
		//is selected
		
		rect_handler=true;
		
		//draw handler for rect setting
		var handler:Sprite=new Sprite();
		handler.graphics.lineStyle(0,0x0066cc,0);
		handler.graphics.beginFill(0x0066cc,1);
		handler.graphics.drawCircle(0,0,5);
		handler.graphics.endFill();
		
		var handler2:Sprite=new Sprite();
		handler2.graphics.lineStyle(0,0x0066cc,0);
		handler2.graphics.beginFill(0x0066cc,1);
		handler2.graphics.drawCircle(0,0,5);
		handler2.graphics.endFill();
		
		var rect:Rect=canvas.getRect2(point);
		focusRect=rect;
		
		
		var corner:Array=canvas.getCorner(point);
		var p1:Object=canvas.data[corner[1]];
		var p2:Object=canvas.data[corner[2]];
		
		
		var dx1:Number=Math.abs(p1.x-p2.x);
		var dy1:Number=Math.abs(p1.y-p2.y);
		
		if(dx1<dy1){
			handler.x=point.x+rect.distance;
			handler.y=point.y;
			
			handler2.x=point.x;
			handler2.y=point.y+rect.height/2;
		}else{
			handler.x=point.x;
			handler.y=point.y+rect.distance;
			
			handler2.x=point.x-rect.height/2;
			handler2.y=point.y;
		}
		
		dx=dx1;
		dy=dy1;
		
		focusPoint=point;
		
		handler.addEventListener(MouseEvent.MOUSE_DOWN,downHandler);
		handler2.addEventListener(MouseEvent.MOUSE_DOWN,downHandler2);
		mouse_mc.addChild(handler);
		mouse_mc.addChild(handler2);
	}
	
	
	public function downHandler2(evt:MouseEvent):void{
		caughtDown=true;
		resetting=true;
		canvas.stage.addEventListener(MouseEvent.MOUSE_MOVE,moveHandler2);
		canvas.stage.addEventListener(MouseEvent.MOUSE_UP,upHandler);
	}
	
	public function downHandler(evt:MouseEvent):void{
		caughtDown=true;
		resetting=true;
		canvas.stage.addEventListener(MouseEvent.MOUSE_MOVE,moveHandler);
		canvas.stage.addEventListener(MouseEvent.MOUSE_UP,upHandler);
	}
	
	public function upHandler(evt:MouseEvent):void{
		caughtDown=false;
		resetting=false;
		canvas.stage.removeEventListener(MouseEvent.MOUSE_MOVE,moveHandler);
		canvas.stage.removeEventListener(MouseEvent.MOUSE_MOVE,moveHandler2);
		canvas.stage.removeEventListener(MouseEvent.MOUSE_UP,upHandler);
	}
	
	public function moveHandler(evt:MouseEvent):void{
		var p:Object=canvas.simpleMousePoint();
		if(dx<dy){
			focusRect.distance=p.x-focusPoint.x;
		}else{
			focusRect.distance=p.y-focusPoint.y;
		}
	}
	
	public function moveHandler2(evt:MouseEvent):void{
		var p:Object=canvas.simpleMousePoint();
		if(dx<dy){
			focusRect.height=(p.y-focusPoint.y)*2;
		}else{
			focusRect.height=(focusPoint.x-p.x)*2;
		}
	}
	
	/*
	override public function renderMouseMC(mouse_mc:Sprite):void{
		var p:Item=canvas.getClosePoint();
		var ps:Array=canvas.getCloseLine();
		
		canvas.removeChildren(mouse_mc);
		if(p){
			mouse_mc.addChild(getHandler(p));
		}
		
		if(select_rect){
			//draw the select rect
			var s:Sprite=new Sprite();
			var g:Graphics=s.graphics;
			g.lineStyle(1,0x0066cc,1);
			g.beginFill(0x0066cc,0.1);
			g.drawRect(select_rect.x,select_rect.y,select_rect.width,select_rect.height);
			g.endFill();
			mouse_mc.addChild(s);
		}
	}
	*/
	
	override public function upAddPointAction():void{
		if(resetting)return;
		addPoint.id=canvas.getNewId();
		targetLine.splitPath(addPoint);
		
		//add the point, then make it a arc, and add arc to arc collection
		trace("addpoint action");
		canvas.addRect(addPoint);
	}
	
	override public function directUpHandler(target:Object):void{
		
		if(target.isRect){
			var rect:Rect=canvas.getRect2(target);
			rect.flipRect();
		}
		
	}
	
}

}

